﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;


namespace Project_2._2.Controllers
{
    public class ThuongHieuController : Controller
    {
        //
        // GET: /ThuongHieu/

        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var th = (from p in db.ThuongHieux where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                        ViewBag.record = rd;
                        return View(th);
                    }
                    else
                    {
                        var th = (from p in db.ThuongHieux where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, 3);
                        return View(th);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            int record = 0;
            if (string.IsNullOrEmpty(col["KeyWord"]))
            {
                record = int.Parse(col["Record"]);
                var th = (from p in db.ThuongHieux where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                ViewBag.record = col["Record"];
                return View(th);
            }
            else
            {
                string SearchBy = col["SearchBy"].ToString();
                string key = col["KeyWord"].ToString();

                switch (SearchBy)
                {
                    case "TenThuongHieu":
                        var th = (from p in db.ThuongHieux where p.isDelete == false & p.TenThuongHieu.Contains(key) select p).ToList().ToPagedList(page ?? 1, 1);
                        ViewBag.record = "1";
                        return View(th); break;
                }
                return View();
            }
        }
        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> multi_delete_thuonghieu)
        {
            var th_delete = db.ThuongHieux.Where(x => multi_delete_thuonghieu.Contains(x.ThuongHieuId)).ToList();
            foreach (var dv in th_delete)
            {
                dv.isDelete = true;
                UpdateModel(dv);
                db.SaveChanges();
            }
            return RedirectToAction("Default", "ThuongHieu");
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(ThuongHieu_ViewModels thuonghieu, HttpPostedFileBase uploadFile)
        {
            if (ModelState.IsValid)
            {
                ThuongHieu th = new ThuongHieu();
                th.TenThuongHieu = thuonghieu.TenThuongHieu;
                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    var path = Path.Combine(Server.MapPath("~/Images/LoGo/"), fileName); uploadFile.SaveAs(path);
                    th.Logo = fileName;
                }
                th.isDelete = false;
                db.ThuongHieux.Add(th);
                db.SaveChanges();
                ModelState.Clear();
                thuonghieu = new ThuongHieu_ViewModels();
                ModelState.AddModelError("", "Bạn đã thêm thành công...!");  // hien thi thong bao
            }
            return View(thuonghieu);
        }
        public ActionResult Delete(int id)
        {
            ThuongHieu th = (from p in db.ThuongHieux where p.ThuongHieuId == id select p).First();
            th.isDelete = true;
            UpdateModel(th);
            db.SaveChanges();
            return RedirectToAction("Default", "ThuongHieu");
        }
        public ActionResult Update(int id)
        {
            var th = (from p in db.ThuongHieux where p.ThuongHieuId == id select p).First();
            return View(th);
        }
        [HttpPost]
        public ActionResult Update(int id, FormCollection col, HttpPostedFileBase uploadFile)
        {
            var th = (from p in db.ThuongHieux where p.ThuongHieuId == id select p).First();
            th.TenThuongHieu = col["TenThuongHieu"];
            //Upload hinh anh
            if (Request.Files.Count < 0)
            {
                ModelState.AddModelError("", "Vui long chon logo");
            }
            else if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                var fileName = Path.GetFileName(uploadFile.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/LoGo/"), fileName); uploadFile.SaveAs(path);
                th.Logo = fileName;
            }
            UpdateModel(th);
            db.SaveChanges();
            ModelState.AddModelError("", "Cập nhật thành công.");
            return RedirectToAction("Default");
        }
    }
}
